﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Senthil.Mobile.WhereAmI
{
    public partial class RouteSelectorForm : Form
    {
        public RouteSelectorForm()
        {
            InitializeComponent();
        }

        private void RouteSelectorForm_Load(object sender, EventArgs e)
        {
            foreach (string route in RouteManager.RouteFiles)
            {
                routesList.Items.Add(new ListViewItem(System.IO.Path.GetFileNameWithoutExtension(route)));
            }

            if (routesList.Items.Count == 0)
            {
                MessageBox.Show("No routes available to load. Please copy route files to " + RouteManager.appDataDirectory + " and click Options->Select Route.", this.Text);
                this.DialogResult = DialogResult.Cancel;
            }
        }

        internal string SelectedRoute
        {
            get 
            {
                return routesList.SelectedIndices.Count > 0 ? routesList.Items[routesList.SelectedIndices[0]].Text : null; 
            }
        }

        private void routesComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //descriptionTextBox.Text = string.Format("{0} - From {1} to {2} via {3}", SelectedRoute.Name, SelectedRoute.Start.Name, SelectedRoute.Destination.Name,
            //    SelectedRoute.ModeOfTravel);
        }

        private void okMenuItem_Click(object sender, EventArgs e)
        {
            AcceptUserSelection();
        }

        private void AcceptUserSelection()
        {
            if (routesList.SelectedIndices.Count == 0)
            {
                MessageBox.Show("Please select a route.", this.Text);
                return;
            }

            this.DialogResult = DialogResult.OK;
        }

        private void cancelMenuItem_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void RouteSelectorForm_KeyDown(object sender, KeyEventArgs e)
        {   
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                AcceptUserSelection();
            }
        }
    }
}